from typing import List

from pydantic import BaseModel, Field

from .public import AssertDataModel


class RequestDataModel(BaseModel):
    method: str
    url: str
    params: dict = None
    data: dict = None
    headers: dict = None
    cookies: dict = None
    files: dict = None
    auth: tuple = None
    timeout: int = None
    allow_redirects: bool = True
    proxies: dict = None
    hooks: dict = None
    stream: bool = None
    verify: str = None
    cert: str = None
    json_: dict = Field(None, alias='json')


class APICaseDataModel(BaseModel):
    module_name: str = Field(alias='模块名称')
    api_name: str = Field(alias='接口名称')
    case_name: str = Field(alias='用例名称')
    case_description: str = Field('', alias='用例描述')
    case_tags: str = Field('', alias='用例标签')
    is_run: bool = Field(True, alias='是否运行')
    request_data: RequestDataModel = Field(alias='请求数据')
    assert_data: List[AssertDataModel] = Field([], alias='断言')
    extract: dict = Field(None, alias='提取数据')


def api_case_data_validate(data: dict) -> APICaseDataModel:
    try:
        case_data = APICaseDataModel(**data)
        return case_data
    except Exception as e:
        raise ValueError(f"数据校验失败: {e}")
